<?php

namespace common\models\business;

use Yii;

/**
 * This is the model class for table "business_contract_purchase".
 *
 * @property string $id
 * @property integer $contract_id
 * @property string $name
 * @property integer $num
 * @property integer $createtime
 * @property string $pirce
 */
class BusinessContractPurchase extends \yii\db\ActiveRecord
{


    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '合同购进单',      //用于修改了什么的显示
        'mainfield' => 'name',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];


    const STATUS_DEFAULT = 0;
    const STATUS_AGREE = 1;
    const STATUS_FINISH = 2;
    public static $statues = [
        self::STATUS_DEFAULT => '未审核',
        self::STATUS_AGREE => '已审核',
        self::STATUS_FINISH => '已结算',

    ];


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_contract_purchase';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['contract_id', 'number', 'num', 'price'], 'required'],
            [['contract_id', 'num','status'], 'integer'],
            [['price'], 'number'],
            [['name','agreeuser','inputuser'], 'string', 'max' => 30],
            [['number'], 'string', 'max' => 50],
            [['connect_tel','connect_person'],'string','max'=>20],
            [['connect_address'],'string','max'=>100],
            [['remark'], 'string', 'max' => 500],
            [['file'], 'image', 'extensions' => 'jpg, jpeg, gif, png', 'on' => ['insert', 'update']],
            ['createtime','safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'contract_id' => '合同id',
            'name' => '购进单名称',
            'num' => '数量',
            'createtime' => '创建时间',
            'price' => '金额',
            'file' => '附件',
            'pigs'=> '包含仔猪',
            'number' => '订购单号',
            'name' => '订购单名称',
            'remark' => '备注',
            'agreeuser' => '审核人',
            'status' => '状态',
            'inputuser' => '执单人',
            'connect_person' => '购进联系人',
            'connect_tel' => '联系电话',
            'connect_address' => '联系地址',
        ];
    }


    public function beforeSave($insert)
    {
        if(empty($this->createtime)){
            $this->createtime = time();
        }
        $this->createtime = strtotime($this->createtime);

        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }


    /**
     * @return \yii\db\ActiveQuery
     * 关联的合同
     */
    public function getContract()
    {
        return $this->hasOne(BusinessContract::className(),['id'=>'contract_id']);
    }


    /**
     * @return $this
     * 获取结算单
     */
    public function getStatements()
    {
        return $this->hasOne(BusinessContractStatements::className(), ['id' => 'sta_id']);
    }




    //获取购进单的猪仔
    public function getPigs()
    {
        return $this->hasMany(BusinessPig::className(),['contract_purid'=>'id']);
    }


}
